package com.cpimex.eprice.action;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;

import org.apache.struts.action.ActionForward;
import com.cpimex.eprice.action.plugin.ConnectionBuilder;
import com.cpimex.eprice.forms.ListadoActionForm;
import com.cpimex.eprice.model.users.Usuario;
import org.apache.struts.action.Action;
import javax.naming.*;
//import javax.sql.*;
import java.sql.*;
import java.util.*;

public class ListadoAction extends Action {
    public ActionForward execute(ActionMapping actionMapping,
                                 ActionForm actionForm,
                                 HttpServletRequest servletRequest,
                                 HttpServletResponse servletResponse) throws Exception {
        InitialContext context = new InitialContext();
        //DataSource dataSrc = (DataSource)context.lookup("java:comp/env/jdbc/Eprice");
      //Connection conn = ConnectionBuilder.epriceConnection(context);
    	DataSource dataSrc = (DataSource) context.lookup("java:comp/env/jdbc/Eprice");
    	Connection conn = dataSrc.getConnection();
    	
    	        Statement stmt = conn.createStatement();
        try{
            ListadoActionForm listadoActionForm = (ListadoActionForm) actionForm;

            ArrayList arr_registros, arr_estatus, arr_perfilGte, arr_perfilCoord, arr_perfilEjec;

            String str_estatus    =  listadoActionForm.getStr_estatus();
            String str_cliente    =  listadoActionForm.getStr_cliente();
            String str_prod       =  listadoActionForm.getStr_producto();
            String str_coord      =  listadoActionForm.getStr_coord();
            String str_gerente    =  listadoActionForm.getStr_gerente();
            String str_ejecutivo  =  listadoActionForm.getStr_ejecutivo();
            int int_orden         =  listadoActionForm.getInt_orden();
            boolean bool_asc      =  listadoActionForm.getBool_asc();

            if(int_orden==0){
                int_orden = 1;
            }

            SortedMap map = new TreeMap();

            map.put("str_estatus", str_estatus );
            map.put("str_cliente", str_cliente );
            map.put("str_prod"   , str_prod );
            map.put("str_coord"  , str_coord );
            map.put("str_gerente", str_gerente );
            map.put("str_ejecutivo"    , str_ejecutivo );

            HttpSession session = servletRequest.getSession();

            Usuario usr = (Usuario)session.getAttribute("usuario");

            if(usr == null){
                stmt.close();
                conn.close();
                return actionMapping.findForward("sesionInvalida");
            }

            arr_registros   =  usr.listado(conn, "", map, int_orden, bool_asc);
            arr_estatus     =  usr.listarEstatus(conn);

            arr_perfilGte   =  usr.listarGerente(conn, Usuario.GERENTE);
            arr_perfilCoord =  usr.listarCoordinador(conn, Usuario.COORDINADOR);
            arr_perfilEjec =  usr.listarPerfil(conn, Usuario.EJECUTIVO);

            // Atributo de resultado de listado
            servletRequest.setAttribute("listado" ,arr_registros);

            // Atributos de combobox
            servletRequest.setAttribute("estatus" ,arr_estatus);
            servletRequest.setAttribute("perfilGte" ,arr_perfilGte);
            servletRequest.setAttribute("perfilCoord" ,arr_perfilCoord);
            servletRequest.setAttribute("perfilEjec" ,arr_perfilEjec);

            // Atributos para links de encabezado de columna
            servletRequest.setAttribute("codCliente", usr.ordenarColumna(bool_asc, int_orden, 1, map));
            servletRequest.setAttribute("descrCli", usr.ordenarColumna(bool_asc, int_orden, 2, map));
            servletRequest.setAttribute("codProd", usr.ordenarColumna(bool_asc, int_orden, 3, map));
            servletRequest.setAttribute("descrPro", usr.ordenarColumna(bool_asc, int_orden, 4, map));
            servletRequest.setAttribute("fecha", usr.ordenarColumna(bool_asc, int_orden, 5,map));
            servletRequest.setAttribute("status", usr.ordenarColumna(bool_asc, int_orden, 6, map));

            return actionMapping.findForward("listado");
        }catch(Exception ex){
            throw ex;
        }finally{
            stmt.close();
            conn.close();
        }

    }
}
